package com.example.duckshop.ui.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.duckshop.R;
import com.example.duckshop.bean.Shop;
import com.example.duckshop.util.MySqliteOpenHelper;
import com.example.duckshop.util.SPUtils;
import com.example.duckshop.widget.ActionBar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 明细信息
 */
public class ShopDetailActivity extends AppCompatActivity {
    MySqliteOpenHelper helper = null;
    private Activity mActivity;
    private ImageView ivImg;
    private TextView tvTitle;
    private TextView tvDate;
    private TextView tvContent;
    private TextView tvPrice;
    private ActionBar mActionBar;//标题栏
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Integer userId;
    private SQLiteDatabase db;
    private Shop shop;
    private int collectCount = 0;
    private LinearLayout llEmpty;
    private Button btnBuy;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = this;
        setContentView(R.layout.activity_shop_detail);
        helper = new MySqliteOpenHelper(this);
        ivImg = findViewById(R.id.img);
        tvTitle = findViewById(R.id.title);
        tvDate = findViewById(R.id.date);
        tvContent = findViewById(R.id.content);
        tvPrice = findViewById(R.id.price);
        llEmpty = findViewById(R.id.ll_empty);
        btnBuy = findViewById(R.id.btn_buy);
        mActionBar = findViewById(R.id.myActionBar);
        //菜单
        mActionBar.setData(mActivity, "明细信息", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        userId = (Integer) SPUtils.get(mActivity, SPUtils.USER_ID, 0);
        shop = (Shop) getIntent().getSerializableExtra("shop");
        tvTitle.setText(shop.getTitle());
        tvDate.setText(shop.getDate());
        tvPrice.setText(String.format("¥ %s", shop.getPrice()));
        tvContent.setText(shop.getContent());
        Glide.with(mActivity)//获得Glide对象的实例，mActivity是当前Activity的上下文对象
                .asBitmap()//将图片作为一个Bitmap对象加载
                .skipMemoryCache(true)//不将图片缓存到内存中
                .load(shop.getImg())//需要加载的图片的地址，shop.getImg()返回图片的URL地址
                .diskCacheStrategy(DiskCacheStrategy.NONE)//不将图片缓存到磁盘中
                .into(ivImg);//将图片展示在一个ImageView组件上
        db = helper.getWritableDatabase();
    }


    //收藏
    public void buy(View view) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(mActivity); //展示一些提示信息或者与用户进行交互的对话框
        dialog.setMessage("确认要收藏该菜品吗");
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                SQLiteDatabase db = helper.getWritableDatabase();
                String ordersSql = "insert into orders (userId,shopId,amount,date) values(?,?,?,?)";
                db.execSQL(ordersSql, new Object[]{userId, shop.getId(), shop.getPrice(),sf.format(new Date())});
                db.close();
                Toast.makeText(mActivity, "收藏成功", Toast.LENGTH_SHORT).show();
            }
        });
        dialog.setNeutralButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
}
